package com.iwuke.dance.wallet.mind_value;

import android.view.View;

import com.google.gson.Gson;
import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.DefaultView;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.LogUtil;
import com.iwuke.dance.util.SharedPreferencesUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.iwuke.dance.wallet.mind_value.adapter.MindValueDetailsAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/29
 * 心意值明细
 */
public class MindValueDetailsActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private RefreshLayout refreshLayout;
    private RecyclerView recycler_view;
    private DefaultView default_view;
    private List<MindValueDetailsBean> lists = new ArrayList<>();
    private int pageNum;
    private MindValueDetailsAdapter detailsAdapter;


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(MindValueDetailsActivity.this,networkEvent,null);

    }

    @Override
    protected int getView() {
        return R.layout.activity_mind_value_details;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);



        title_bar.setTitleText("心意值明细");
        title_bar.setLeftOnClick(this);

    }

    @Override
    protected void initData() {
        refreshLayout = findViewById(R.id.refresh_layout);
        recycler_view = findViewById(R.id.recycler_view);
        //默认视图
        default_view = findViewById(R.id.default_view);

        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                pageNum=1;
                requestBalance();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
//                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                pageNum++;
                requestBalance();
            }
        });

        pageNum=1;
        requestBalance();
    }

    private void requestBalance() {

        Map<String, String> map = new HashMap<>();
        map.put("walletId", SharedPreferencesUtil.getInstance().getWalletId());//钱包id
        map.put("page",String.valueOf(pageNum));

        PathUrl.mindValueDetails(map, new PathUrl.DataCallBackList<MindValueDetailsBean>() {
            @Override
            public void onSucessList(List<MindValueDetailsBean> list) {

                //加载失败页面
                Util.setRefreshlayout(refreshLayout);//下来加载 上拉刷新 没有更多数据了
                default_view.setVisibility(View.GONE);//隐藏默认页

                if (pageNum==1&&lists.size()>0){
                    lists.clear();
                }

                if (null!=list&&list.size()!=0){
                    if (pageNum==1){
                        lists.addAll(list);
                    }else if (list.size()==1){//只查询出一个月粉中的数据
                        if (list.get(0).month.equals(lists.get(lists.size()-1).month)){//同月份
                            lists.get(lists.size()-1).transactions.addAll(list.get(0).transactions);
                        }else {//从上个月开始的，上个月数据
                            lists.addAll(list);
                        }
                    }else if (list.size()>1){//查询出两个或多个月份中的数据
                        LogUtil.logD("","查询出两个或多个月份中的数据");
                        if (list.get(0).month.equals(lists.get(lists.size()-1).month)){//同月份数据
                            LogUtil.logD("同月份数据","同月份数据");
                            lists.get(lists.size()-1).transactions.addAll(list.get(0).transactions);
                            LogUtil.logD("同月份数据list移除前",new Gson().toJson(list));
                            list.remove(0);
                            LogUtil.logD("同月份数据list移除后",new Gson().toJson(list));
                            lists.addAll(list);
                            LogUtil.logD("同月份数据",new Gson().toJson(lists));
                        }else {//从上个月开始的多个月份中的数据
                            lists.addAll(list);
                            LogUtil.logD("从上个月开始的多个月份中的数据","同月份数据");
                        }
                    }
                    refreshLayout.setEnableLoadMore(true);//有数据上拉加载成功
                }else {
                    if (pageNum==1){
                        lists.clear();
                    }

                    if (lists==null||lists.size()==0){
                        default_view.setTextView("没有数据~");
                        default_view.setVisibility(View.VISIBLE);
                    }
                    if (pageNum>1){//当前页没数据，页数减一
                        LogUtil.logD("没有更多数据了","没有更多数据了");
                        pageNum--;
                        refreshLayout.setNoMoreData(true);//没有更多数据了
                    }
                }

                if (detailsAdapter==null){
                    setLinearLayoutManager(MindValueDetailsActivity.this,recycler_view);
                    detailsAdapter = new MindValueDetailsAdapter(MindValueDetailsActivity.this, R.layout.adapter_mind_value_details, lists);
                    recycler_view.setAdapter(detailsAdapter);
                }else {
//                    detailsAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(String errorCode) {
                Util.setRefreshlayout(refreshLayout);//下来加载 上拉刷新 没有更多数据了
                default_view.setTextView(errorCode);
                default_view.setVisibility(View.VISIBLE);
            }
        });
    }
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){//返回
            finish();
        }
    }
}
